package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import com.joker.orange.domain.ChapterBuy;
import com.joker.orange.mapper.ChapterBuyMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.BuyOrderBo;
import com.joker.orange.domain.vo.BuyOrderVo;
import com.joker.orange.domain.BuyOrder;
import com.joker.orange.mapper.BuyOrderMapper;
import com.joker.orange.service.IBuyOrderService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 购买订单Service业务层处理
 *
 * @author Joker
 * @date 2024-07-08
 */
@RequiredArgsConstructor
@Service
public class BuyOrderServiceImpl implements IBuyOrderService {

    private final BuyOrderMapper baseMapper;
    private final ChapterBuyMapper chapterBuyMapper;

    @Override
    public BuyOrder queryByProductId(Long productId, Long chapterId, String orderType) {
        return baseMapper.selectOne(new LambdaQueryWrapper<BuyOrder>()
            .eq(BuyOrder::getProductId, productId)
            .eq(ObjectUtil.isNotNull(chapterId), BuyOrder::getChapterId, orderType)
            .eq(BuyOrder::getOrderType, orderType));
    }

    /**
     * 查询购买订单
     */
    @Override
    public BuyOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询购买订单列表
     */
    @Override
    public TableDataInfo<BuyOrderVo> queryPageList(BuyOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BuyOrder> lqw = buildQueryWrapper(bo);
        Page<BuyOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询购买订单列表
     */
    @Override
    public List<BuyOrderVo> queryList(BuyOrderBo bo) {
        LambdaQueryWrapper<BuyOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BuyOrder> buildQueryWrapper(BuyOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BuyOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, BuyOrder::getUserId, bo.getUserId());
        lqw.eq(bo.getProductId() != null, BuyOrder::getProductId, bo.getProductId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), BuyOrder::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductCover()), BuyOrder::getProductCover, bo.getProductCover());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), BuyOrder::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderAmount() != null, BuyOrder::getOrderAmount, bo.getOrderAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderStatus()), BuyOrder::getOrderStatus, bo.getOrderStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderType()), BuyOrder::getOrderType, bo.getOrderType());
        lqw.orderByDesc(BuyOrder::getCreateTime);
        return lqw;
    }

    /**
     * 新增购买订单
     */
    @Override
    public Boolean insertByBo(BuyOrderBo bo) {
        BuyOrder add = BeanUtil.toBean(bo, BuyOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改购买订单
     */
    @Override
    public Boolean updateByBo(BuyOrderBo bo) {
        BuyOrder update = BeanUtil.toBean(bo, BuyOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BuyOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除购买订单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 小说/漫画购买掌机记录
     */
    @Override
    public void insertChapterBuy(ChapterBuy chapterBuy) {
        chapterBuyMapper.insert(chapterBuy);
    }
}
